<?php


namespace App\Http\Services\Manage;

use App\Models\AdminUser;
use App\Models\Contact;
use App\Models\Team;
use App\Models\TeamAdmin;
use App\Repositories\TeamRepository;

/**
 * Class AdminService
 * @package App\Http\Services\Manage
 */
class AdminService
{
    public function __construct()
    {
    }

    /**
     * 获取admin的信息，包括team相关信息，admin相关信息，以及当前admin的权限等等
     *
     * @param $adminId
     * @param $teamAdminId
     *
     * @return
     */
    public function getAdminAndTeamInfo($adminId, $teamAdminId)
    {
        $ret = [];
        $admin = AdminUser::noTeamLimit()->find($adminId);
        if ($admin->super_user === AdminUser::SUPER_USER && !$teamAdminId) {
            return $admin;
        } else {
            $admin['super_user'] = 0;
        }
        $teamAdmin = TeamAdmin::noTeamLimit()->find($teamAdminId);
        if (!$teamAdmin || $teamAdmin->status === TeamAdmin::STATUS_UNUSED || !$admin) {
            return $ret;
        }
        $team = Team::noTeamLimit()->find($teamAdmin->team_id);

        $ret = $teamAdmin;
        $ret->admin = $admin;
        $ret->roleOption = [
            'activity.insert' => true,
            'activity.update' => true,
            'activity.select' => true,
            'activity.delete' => true,
            'activity.review' => false,

            'activitySession.insert' => true,
            'activitySession.update' => true,
            'activitySession.select' => true,
            'activitySession.delete' => true,

            'contact.select' => true,
            'team.select' => true,
        ];

        $ret->team_ids = [$teamAdmin->team_id];
        if ($ret->role === TeamAdmin::ROLE_ADMIN) { // 如果是管理员（机构管理员或子机构管理员）
            $ret->roleOption = array_merge(
                $ret->roleOption,
                [
                    'contact.insert' => true,
                    'contact.update' => true,
                    'contact.delete' => true,
                    'activity.review' => true,
                ]
            );
        }

        if ($team->parent_id === 0 && $ret->role === TeamAdmin::ROLE_ADMIN) { // 如果是机构管理员
            $ret->roleOption = array_merge(
                $ret->roleOption,
                [
                    'team.insert' => true,
                    'team.update' => true,
                    'team.delete' => true,
                ]
            );

            $teamRepository = new TeamRepository();
            $ret->team_ids = $teamRepository->getManageTeamIds($teamAdmin->team_id);
        }

        return $ret;
    }

    public function getByUnionId($unionId)
    {
        $contact = Contact::where('unionid', $unionId)->first();
        if ($contact) {
            return $contact['admin'];
        } else {
            return false;
        }
    }

    public function getRootTeam($team)
    {
        if ($team['parent_id'] == 0) {
            return $team;
        }
        $parent = $team;
        foreach (range(0, 3) as $i) {
            $parent = $parent['parent'];
            if ($parent['parent_id'] == 0) {
                return $parent;
            }
        }
        return null;
    }
}
